// pages/caselist/caselist.js
const allUrl = require('../../config').allUrl
var app = getApp()
// var openId = null

Page({
  data:{
    currentPage: 1,
    pageCount: 10,
    waitInfo:[],
          // call_id:"11"
          // openid:"o1oL70HabL7LOqdJzrmMkxTkvmFw"
          // j_id:"0"
          // mobile:"15637196121"
          //  {address:"北京市东城区正义路2号正义路"
          // lat:"39.90403"
          // lng:"116.407526"
          // status:"0"} 
          // created_at:"2017-05-05 14:23:28"
          // done_at:"0000-00-00 00:00:00"
          // remark:"这是一条备注"
    isRequesting: false, //是否正在请求
    isAllData: false,

  },


  onLoad:function(options){
    //如果民警没有登录的话，就让他去登录界面
  },
  onReady:function(){
    // 页面渲染完成
  },
  onShow:function(){
    // 页面显示
    if(app.globalData.j_id){
      this.onPullDownRefresh()
    }else{
      wx.switchTab({
        url: '../bind/bind',
        success: function(res){},
        fail: function(res) {},
        complete: function(res) {
          wx.showToast({
            title:'未登录，请先登陆再查看案件列表'
          })
        }
      })
    }
  },
  onHide:function(){
    // 页面隐藏
  },
  onUnload:function(){
    // 页面关闭
  },
  //下拉刷新
  onPullDownRefresh: function(){
    var that = this
    // console.log('下拉刷新')
    if(this.data.isRequesting){
      wx.stopPullDownRefresh()
      return;
    }
    this.setData({
      currentPage: 1,
      isRequesting: true
    })
    wx.showToast({
      title: '正在刷新...',
      icon: 'loading',
      duration: 10000
    })
    this.onceReq(function(res){
      that.setData({
        isRequesting: false
      })
      wx.stopPullDownRefresh()
      wx.hideToast()
    })
  },
  //上拉加载
  onReachBottom: function(e){
    var that = this
    console.log('circle 下一页')
    if(this.data.isRequesting==false){
      if(this.data.isAllData){
        if(this.data.waitInfo.length>4){
          wx.showToast({
            title: "没有更多数据"
          })
        }
        return;
      }
      this.setData({
        currentPage: this.data.currentPage+1,
        isRequesting: true
      })
      wx.showToast({
        title: '加载更多...',
        icon: 'loading'
      })
      this.onceReq(function(res){
        that.setData({
          isRequesting: false
        })
        wx.stopPullDownRefresh()
        wx.hideToast()
      })
    }
  },

  //  一次请求记录列表
  onceReq: function(complete){
    var that = this
    wx.request({
      url: allUrl + 'Api/getListByPolice.html',
      header: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        j_id: app.globalData.j_id,
        currentPage: that.data.currentPage,
        pageCount: that.data.pageCount
      },
      method: 'POST', 
      success: function(res){
        // console.log(res)
        if(res.data.status_code!=200){
          wx.showToast({
            title: res.data.msg+'',
          })
          complete(res)
          return;
        }

        var tArr = []

        if(that.data.currentPage == 1){//当是在首页的时候，直接替换数据源
          tArr = res.data.data.do ? (res.data.data.do).concat(res.data.data.done) : res.data.data.done ? res.data.data.done:[]
          that.setData({
            waitInfo: tArr
          })
        }else{  //  向以前的数组中添加数据
          var t = that.data.waitInfo.concat(res.data.data.done)
          // var p = (res.data.data.do).concat(t)
          that.setData({
            waitInfo: t
          })
        }

        //如果已经完全返回完，那么就记录
        that.setData({
          isAllData: that.data.waitInfo.length==res.data.data.count
        })


      },
      fail: function(res) {
        wx.showToast({
          title: '请求失败'
        })
      },
      complete: function(res) {
        complete(res)}
    })
  },


  //  查看详情
  lookDetail: function(e){
    // console.log('点击查看详情')

    var index = e.currentTarget.dataset.index
    var oneCase = this.data.waitInfo[index]
    // console.log(oneCase)

    var that = this
    wx.navigateTo({
      url: '../case/case?latitude=' + oneCase.lat + '&longitude=' + oneCase.lng + '&isDone=' + oneCase.status + '&alertPhone=' + oneCase.mobile + '&address=' + oneCase.address + '&remark=' + oneCase.remark + '&created_at=' + oneCase.created_at + '&call_id=' + oneCase.call_id,
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  }
})
